package com.edooteach.api.servcice.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.edoomath.entity.user.User;
import com.edoomath.entity.user.UserAddress;
import com.edooteach.api.dao.UserAddressApiDao;
import com.edooteach.api.dao.UserDao;
import com.edooteach.api.dto.UserAddressEdit;
import com.edooteach.api.servcice.UserAddressApiService;

/**
 * 
 * @author sjh
 *
 */
@Service("userAddressApiService")
public class UserAddressApiServiceImpl extends DataServiceImpl<UserAddress> implements UserAddressApiService{

	@Dao
	private UserAddressApiDao userAddressApiDao;
	
	@Dao
	private UserDao userDao;
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	/*
	 * (non-Javadoc)
	 * @see com.edooteach.api.servcice.UserAddressApiService#list(java.lang.String)
	 */
	public List<UserAddress> list(String unionid) {
		
	return userAddressApiDao.listByunionid(unionid);
	
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edooteach.api.servcice.UserAddressApiService#loadDefault()
	 */
	public UserAddress loadDefault(String unionid) {
	    UserAddress userAddress = genericDaoSupport.searchForObject("FROM UserAddress WHERE selected = true AND unionid = :unionid", "unionid", unionid, UserAddress.class);
	    if(userAddress == null) {
	        userAddress = genericDaoSupport.searchForObject("FROM UserAddress WHERE unionid = :unionid", "unionid", unionid, UserAddress.class);
	    }
	    return userAddress;
	}
	
	/**
	 * 
	 */
	public UserAddress setDefault(Long addressId, String unionid) {
	    UserAddress userAddress = this.loadDefault(unionid);
	    userAddress.setSelected(false);
	    genericDaoSupport.update(userAddress);
	    
	    UserAddress newDefaultAddress = this.load(UserAddress.class, addressId);
	    newDefaultAddress.setSelected(true);
	    genericDaoSupport.update(userAddress);
	    
	    return newDefaultAddress;
	};
	
	/*
	 * (non-Javadoc)
	 * @see com.edooteach.api.servcice.UserAddressApiService#delete(java.lang.Long)
	 */
	public void delete(Long id) {
		
		genericDaoSupport.delete(UserAddress.class, id);
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edooteach.api.servcice.UserAddressApiService#edit(java.lang.Long, com.edooteach.api.dto.UserAddressEdit)
	 */
	public void edit(Long addressId, UserAddressEdit userAddressEdit) {
	    UserAddress userAddress = this.load(UserAddress.class, addressId);
	    userAddress.edit(userAddressEdit.getName(), userAddressEdit.getPhone(), userAddressEdit.getProvince(), userAddressEdit.getCity(), userAddressEdit.getCountry(), userAddressEdit.getDetail());
	    genericDaoSupport.update(userAddress);
	}

	/*
	 * (non-Javadoc)
	 * @see com.edooteach.api.servcice.UserAddressApiService#create(com.edooteach.api.dto.UserAddressRequest)
	 */
	public void create(String unionid, UserAddressEdit userAddressEdit) {
		
	    User user = userDao.loadUser(unionid);
	    if(user == null) {
	        throw BusinessException.newInstance("500", "请先绑定手机");
	    }
	    
	    UserAddress userAddress = new UserAddress(user, userAddressEdit.getName(), userAddressEdit.getPhone(), userAddressEdit.getProvince(), userAddressEdit.getCity(), userAddressEdit.getCountry(), userAddressEdit.getDetail(), userAddressEdit.isDefault());
		genericDaoSupport.save(userAddress);
	}

}
